﻿namespace EFS.RC.CC.CH.Tables
{
	public static class SurveyQuestions
	{
		/// Returns an object from a data row
		public static EFS.RC.CC.CH.Entities.Surveys.Question FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CC.CH.Entities.Surveys.Question dataObj = new EFS.RC.CC.CH.Entities.Surveys.Question();

			// ID
			dataObj.ID = EFS.DB.DataRow.Int32(row, "ID");

			// CATEGORY_ID
			dataObj.SurveyCategoryID = EFS.DB.DataRow.Int32(row, "SURVEY_CATEGORY_ID");

			// QUESTION
			dataObj.Text = EFS.DB.DataRow.String(row, "QUESTION");

			// PRIORITY
			dataObj.Priority = EFS.DB.DataRow.Int32(row, "PRIORITY");

			// ANSWER_FLOW_DIRECTION
			System.Int32? nAnswersFlowDirection = EFS.DB.DataRow.Int32(row, "ANSWERS_FLOW_DIRECTION");
			dataObj.AnswersFlowDirection = null;
			if (nAnswersFlowDirection.HasValue)
			{
				dataObj.AnswersFlowDirection = (EFS.RC.CC.CH.Entities.Surveys.Question.eAnswersFlowDirection)nAnswersFlowDirection.Value;
			}

			// ok
			return dataObj;
		}

		/// GetAll
		public static EFS.RC.CC.CH.Entities.Surveys.Question[] GetByCategoryId(System.Int32 categoryId)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Question>(
					"[dbo].[SurveyQuestions_GetBySurveyCategoryID]",
					new EFS.DB.Parameters.Array("survey_category_id", categoryId),
					FromTableRow);
			}
		}

		/// GetByID
		public static EFS.RC.CC.CH.Entities.Surveys.Question GetByID(System.Int32 id)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteRowReaderQuery<EFS.RC.CC.CH.Entities.Surveys.Question>(
					"[dbo].[SurveyQuestions_GetByID]",
					new EFS.DB.Parameters.Array("id", id),
					FromTableRow);
			}
		}

		/// Create
		public static System.Int32? Create(
			System.Int32 categoryId,
			System.String text,
			System.Int32 priority,
			EFS.RC.CC.CH.Entities.Surveys.Question.eAnswersFlowDirection answersflowdirection)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray = new EFS.DB.Parameters.Array(
				new System.String[] { "question", "survey_category_id", "priority", "answers_flow_direction", "id" },
				new System.Object[] { text, categoryId, priority, (System.Int32)answersflowdirection, default(System.Int32) },
				new System.Boolean[] { false, false, false,false, true });

			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// exec create
				connection.DoExecuteNonQuery("[dbo].[SurveyQuestions_Create]", parametersArray);
			}

			// get id value
			return parametersArray.GetOutPrameterValue<System.Int32?>("id"); 
		}

		/// Update
		public static void Update(
			System.Int32 id,
			System.Int32 categoryId,
			System.String text,
			System.Int32 priority,
			EFS.RC.CC.CH.Entities.Surveys.Question.eAnswersFlowDirection answersflowdirection)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery(
					"[dbo].[SurveyQuestions_Update]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "id", "survey_category_id", "question", "priority", "answers_flow_direction" },
						new System.Object[] { id, categoryId, text, priority, (System.Int32)answersflowdirection }));
			}
		}

		/// Delete
		public static void Delete(System.Int32 id)
		{	
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery("[dbo].[SurveyQuestions_Delete]", new EFS.DB.Parameters.Array("id", id));
			}
		}
	}

}